using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Forecast.Business;
using Forecast.Database;

public delegate void TopNewsVisibilityChangedEventHandler(object sender, EventArgs e);

public partial class controls_misc_TopNews : System.Web.UI.UserControl
{
    public event TopNewsVisibilityChangedEventHandler TopNewsVisibilityChanged;
    private Player _player;
    private Prediction _prediction;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void LinkButton_Click(Object sender, EventArgs e)
    {
        if (_showHideNews.Text == "Show")
        {
            _showHideNews.Text = "Hide";
            rptItems.Visible = true;
        }
        else if (_showHideNews.Text == "Hide")
        {
            _showHideNews.Text = "Show";
            rptItems.Visible = false;
        }
        if (TopNewsVisibilityChanged != null)
            TopNewsVisibilityChanged(this, new EventArgs());
    }

    public void BindData()
    {
        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        IList<NewsItem> topNews = activity.GetTopMainNews();
        rptItems.DataSource = topNews;
        rptItems.DataBind();
    }

    public void BindData(Prediction prediction)
    {
        _header.Text = String.Format("Latest news for {0}   (", prediction.Ticker);
        _prediction = prediction;
        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        IList<NewsItem> topNews = activity.GetTopPredictionNews(prediction.Id);
        rptItems.DataSource = topNews;
        rptItems.DataBind();
    }

    public void BindData(Player player)
    {
        _header.Text = String.Format("Latest news for {0}   (", player.UserName);
        _player = player;
        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        IList<NewsItem> topNews = activity.GetTopPlayerNews(player.Id);
        rptItems.DataSource = topNews;
        rptItems.DataBind();
    }

    protected string GetNewsDate(DateTime newsDate)
    {
        return TimespanExtensions.HowLongAgo(newsDate);
    }

    protected string GetNewsAsHtml(string newsMessage)
    {
        return StringExtensions.RenderAsHtml(newsMessage);
    }

    protected string GetNewsArchiveContext()
    {
        string context = "";
        if (_player != null)
            context = " about " + _player.UserName + " ";
        if (_prediction != null)
            context = " about " + _prediction.Ticker + " ";
        return context;
    }

    protected string GetArchive()
    {
        string archive = "News archive";
        if (_player != null)
            archive = _player.UserName + " news archive";
        if (_prediction != null)
            archive = _prediction.Ticker + " news archive";
        return archive;
    }

    protected string GetLink()
    {
        string link = "~/News.aspx";
        if (_player != null)
        {
            link += String.Format("?playerId={0}&context={1}", _player.Id, _player.UserName);
        }
        if (_prediction != null)
            link += String.Format("?predictionId={0}&context={1}", _prediction.Id, _prediction.Ticker);
        return link;
    }
}
